﻿using DataAccess.DTO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace Api.Controllers
{
    public class UploadController : ApiController
    {
        /// <summary>
        /// App上传图片
        /// </summary>
        /// <returns>返回上传图片的相对路径</returns>
      
        //public HttpResponseMessage AgentLoginWxImage()
        //{
        //  var rModel = new Result { State = false, Message = "上传失败" };

        //    //string result = "";
        //    // 检查是否是 multipart/form-data
        //    if (!Request.Content.IsMimeMultipartContent("form-data"))
        //    {
        //        // InvestmentCommon.Log4NetHelper.Log.Error("不是有效的'form-data'类型");
        //        rModel.State = false;
        //        rModel.Message = "不是有效的'form-data'类型";
        //        return ResultHand.OnResult(rModel);
        //    }


          
        //    string path = string.Format("/imagestore/agent/");
        //    string abtPath = HttpContext.Current.Server.MapPath(path);

        //    if (Directory.Exists(abtPath) == false)//如果不存在就创建file文件夹
        //    {
        //        Directory.CreateDirectory(abtPath);
        //    }


        //    //if (!InvestmentCommon.FileHelper.CreateDirectory(abtPath))
        //    //{
        //    //    InvestmentCommon.Log4NetHelper.Log.Error(string.Format("创建目录{0}失败", abtPath));
        //    //    rModel.state = 0;
        //    //    rModel.msg = "创建图片目录失败";
        //    //    return rModel;
        //    //}


        //    string fileName = "";
        //    string ext = "";
        //    string filePath = "";
        //    try
        //    {
        //        HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
        //        HttpRequestBase request = context.Request;//定义传统request对象
        //        HttpFileCollectionBase imgFiles = request.Files;
        //        for (int i = 0; i < imgFiles.Count; i++)
        //        {
        //           // ext = FileHelper.GetExtention(imgFiles[i].FileName);
        //            fileName = imgFiles[i].FileName;// string.Format("{0}{1}", System.Guid.NewGuid().ToString(), ext);
        //            filePath = string.Format("{0}/{1}", path, fileName);
        //            imgFiles[i].SaveAs(abtPath + "\\" + fileName);
        //            imgFiles[i].InputStream.Position = 0;
        //            rModel.Datas = fileName;
        //            rModel.State =true;
        //            rModel.Message = "success";
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        //InvestmentCommon.Log4NetHelper.Log.Error("图片保存失败");
        //        //rModel.state = 0;
        //        //rModel.msg = "图片保存失败";
        //        //return rModel;
        //    }


        //    //result = Newtonsoft.Json.JsonConvert.SerializeObject(rList);
        //    return ResultHand.OnResult(rModel);
        //}
        [HttpPost]
        public HttpResponseMessage AgentLoginWxImage_Base64(ImgDemo model)
        {
            //XYD.Common.Request.GetFormString(data);

            var rModel = new Result { State = false, Message = "上传失败" };
            //将Base64String转为图片并保存
            byte[] arr2 = Convert.FromBase64String(model.data);
            using (MemoryStream ms2 = new MemoryStream(arr2))
            {
                string path = string.Format("/imagestore/agent/");
                string abtPath = HttpContext.Current.Server.MapPath(path);
                if (Directory.Exists(abtPath) == false)//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(abtPath);
                }
                System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
                bmp2.Save(abtPath + model.imgName , System.Drawing.Imaging.ImageFormat.Jpeg);

                rModel.State = true;
                rModel.Datas =model.imgName ;
                rModel.Message = "上传成功";
            }
            return ResultHand.OnResult(rModel);
        }

    }

    public class ImgDemo
    {
        public string data { get;set;}
        public string imgName { get; set; }

    }
}
